package cn.manyufun.myf.ui.user.activity;

import android.content.Context;
import android.content.Intent;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.OnClick;
import cn.manyufun.myf.R;
import cn.manyufun.myf.bean.RegisterBean;
import cn.manyufun.myf.bean.VerifyCodeBean;
import cn.manyufun.myf.common.base.BaseActivity;
import cn.manyufun.myf.common.commonwidget.NormalTitleBar;
import cn.manyufun.myf.ui.user.model.RegisterModel;
import cn.manyufun.myf.ui.user.presenter.RegisterPresenter;

import static cn.manyufun.myf.common.commonutils.CommonUtils.isTelNumValid;

/**
 * @author Kevin Xiao
 * @date Create Date: 2018-02-01
 * @Description
 */
public class RegisterAcitivty extends BaseActivity<RegisterPresenter, RegisterModel> {
    @BindView(R.id.et_username)
    EditText mEtUserName;
    @BindView(R.id.et_verify_code)
    EditText mEtVerifyCode;
    @BindView(R.id.et_password)
    EditText mEtPwd;
    @BindView(R.id.ntb)
    NormalTitleBar ntb;
    @BindView(R.id.tv_send_code)
    TextView mTvSendCode;

    private String mVerifyCode;
    private String mVerifyPhone;
    private VerifyCountDown verifyCountDown;

    @OnClick(R.id.btn_register)
    void register() {
        mEtUserName.setError(null);
        mEtVerifyCode.setError(null);
        mEtPwd.setError(null);


        // Store values at the time of the login attempt.
        String username = mEtUserName.getText().toString();
        String verifyCode = mEtVerifyCode.getText().toString();
        String pwd = mEtPwd.getText().toString();

        boolean cancel = false;
        View focusView = null;

        // Check for a valid email address.
        if (TextUtils.isEmpty(username)) {
            mEtUserName.setError(getString(R.string.error_field_required));
            focusView = mEtUserName;
            cancel = true;
        } else if (username.equals(mVerifyPhone) && !isTelNumValid(username)) {
            mEtUserName.setError(getString(R.string.error_invalid_tel));
            focusView = mEtUserName;
            cancel = true;
        }

        if (TextUtils.isEmpty(verifyCode) || !verifyCode.equals(mVerifyCode)) {
            mEtVerifyCode.setError("验证码不正确。");
            focusView = mEtVerifyCode;
            cancel = true;
        }

        if (TextUtils.isEmpty(pwd) || pwd.length() < 6) {
            mEtPwd.setError("密码必须大于6位");
            focusView = mEtPwd;
            cancel = true;
        }
        if (cancel) {
            focusView.requestFocus();
        } else {
            startProgressDialog();
            mPresenter.requestRegister(username, pwd, verifyCode);
        }
    }

    @OnClick(R.id.tv_send_code)
    void sendCode(View view) {
        // Reset errors.
        mEtUserName.setError(null);
        String username = mEtUserName.getText().toString();
//        String verifyCode = mEtVerifyCode.getText().toString();

        boolean cancel = false;
        View focusView = null;

        // Check for a valid email address.
        if (TextUtils.isEmpty(username)) {
            mEtUserName.setError(getString(R.string.error_field_required));
            focusView = mEtUserName;
            cancel = true;
        } else if (!isTelNumValid(username)) {
            mEtUserName.setError(getString(R.string.error_invalid_tel));
            focusView = mEtUserName;
            cancel = true;
        }

        if (cancel) {
            focusView.requestFocus();
        } else {
            mTvSendCode.setClickable(false);
            mVerifyPhone = username;
            mVerifyCode = "";
            mPresenter.sendVerifyCode(username);
            verifyCountDown = new VerifyCountDown(60 * 1000, 1000);
            verifyCountDown.start();
        }
    }


    public static void startAction(Context context) {
        Intent intent = new Intent(context, RegisterAcitivty.class);
        context.startActivity(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_register;
    }

    @Override
    public void initPresenter() {
        mPresenter.setVM(this, mModel);
    }

    @Override
    public void initView() {
        ntb.setTitleText("注册");
        ntb.setOnBackListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    public void returnVerifyCode(VerifyCodeBean bean) {

        if (bean.getSuccess().equals("1")) {
            mVerifyCode = bean.getVerifyCode();
        } else {
            showLongToast(bean.getErrorMsg());
        }
    }

    public void responseRegister(RegisterBean bean) {
        if (bean.getSuccess().equals("1")) {
            showLongToast("注册成功，请登录！");
            finish();
        } else {
            showLongToast(bean.getErrorMsg());
        }
        stopProgressDialog();
    }


    class VerifyCountDown extends CountDownTimer {
        VerifyCountDown(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {
            mTvSendCode.setText("重新发送");
            mTvSendCode.setClickable(true);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            mTvSendCode.setText("请等待" + millisUntilFinished / 1000 + "秒...");
        }
    }
}
